//
//  UUOrderDetailBasicInfoVC.m
//  UUCharterSupplier
//
//  Created by ZhangAo on 24/02/2018.
//  Copyright © 2018 UUCharter. All rights reserved.
//

#import "UUOrderDetailBasicInfoVC.h"
#import "UUResourceManager.h"
#import "UUIntentOrderDetailVC.h"

@interface UUOrderDetailBasicInfoVC ()

@end

@implementation UUOrderDetailBasicInfoVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    
    CGSize fittingCompressedSize = [self.view systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
    self.preferredContentSize = fittingCompressedSize;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setupUI {
    self.view.backgroundColor = [UIColor whiteColor];
    
    UILabel *priceLabel = [UILabel new];
    priceLabel.textAlignment = NSTextAlignmentCenter;
    priceLabel.textColor = COMMON_TEXT_RED_COLOR;
    priceLabel.font = [UIFont systemFontOfSize:24];
    [self.view addSubview:priceLabel];
    [priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(35);
        make.height.mas_equalTo(45);
        make.leading.trailing.mas_equalTo(0);
    }];
    
    NSMutableAttributedString *priceAttributedString = [[NSMutableAttributedString alloc] initWithString:@"￥"];
    [priceAttributedString appendAttributedString:[[NSAttributedString alloc] initWithString:[self.order.vendorMoney toCurrencyWithoutCurrencySymbol]
                                                                                  attributes:@{
                                                                                               NSFontAttributeName : [UIFont systemFontOfSize:32]
                                                                                               }]];
    priceLabel.attributedText = priceAttributedString;
    
    if (self.order.status == UUOrderStatusComplete
        || self.order.status == UUOrderStatusCancelled
        || self.order.status == UUOrderStatusRefunded
        || self.order.refundStatus == UUOrderRefundStatusInRefunding
        || self.order.status == UUOrderStatusWaitingForService
        || self.order.status == UUOrderStatusInService) {
        UIImage *sealImage;
        BOOL align = NO;
        
        if (self.order.refundStatus == UUOrderRefundStatusInRefunding) {
            sealImage = [UIImage imageNamed:@"OrderDetailSealRefunding"];
            align = YES;
        } else {
            switch (self.order.status) {
                case UUOrderStatusComplete:
                    sealImage = [UIImage imageNamed:@"OrderDetailSealCompleted"];
                    break;
                case UUOrderStatusCancelled:
                    sealImage = [UIImage imageNamed:@"OrderDetailSealCancelled"];
                    break;
                case UUOrderStatusRefunded:
                    sealImage = [UIImage imageNamed:@"OrderDetailSealRefunded"];
                    break;
                case UUOrderStatusInService:
                    sealImage = [UIImage imageNamed:@"OrderDetailSealInService"];
                    align = YES;
                    break;
                case UUOrderStatusWaitingForService:
                    sealImage = [UIImage imageNamed:@"OrderDetailSealWaitingForService"];
                    align = YES;
                    break;
                default:
                    DKAssert(0);
            }
        }
        
        UIImageView *sealImageView = [[UIImageView alloc] initWithImage:sealImage];
        [self.view addSubview:sealImageView];
        [sealImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            if (align) {
                make.centerY.equalTo(priceLabel);
            } else {
                make.top.mas_equalTo(20);
            }
            make.right.mas_equalTo(0);
        }];
    }
    
    UILabel *carTypeLabel = [UILabel new];
    carTypeLabel.text = [GetCarCategoryManager() fetchCarCategoryWithCategoryId:self.order.carCategoryId].name;
    carTypeLabel.textAlignment = NSTextAlignmentCenter;
    carTypeLabel.font = [UIFont systemFontOfSize:18];
    [self.view addSubview:carTypeLabel];
    [carTypeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(0);
        make.height.mas_equalTo(25);
        make.top.equalTo(priceLabel.mas_bottom).offset(16);
    }];
    
    CGFloat formPreferredMaxLayoutWidth = self.view.width - 40;
    
    NSMutableArray<UUFormItemMode *> *items = [NSMutableArray new];
    
    [items addObject:[UUFormItemMode itemWithTitle:@"订单金额" content:[self.order.totalPrice toCurrency]]];
    [items addObject:[UUFormItemMode itemWithTitle:@"订单号" content:self.order.orderSn]];
    
    UUAddressModel *departAddressModel = [self.order departureAddressModel];
    [items addObject:[UUFormItemMode itemWithTitle:@"出发城市" content:[GetCityManager() fetchCityWithCityId:departAddressModel.cityId].simpleName]];
    
    [items addObject:[UUFormItemMode itemWithTitle:@"出发地址" content:self.order.departureAddressModel.address]];
    [items addObject:[UUFormItemMode itemWithTitle:@"出发时间" content:[self.order.departTime formatDateByFormatString:@"yyyy.MM.dd HH:mm"]]];
    [items addObject:[UUFormItemMode itemWithTitle:@"类型" content:self.order.type == UUOrderTypeDay ? @"按天" : (self.order.type == UUOrderTypeInstant ? @"即时报价" : @"按线路")]];
    
    if (self.order.type == UUOrderTypeDay || self.order.type == UUOrderTypeInstant) { // 按天或即时报价
        UUAddressModel *arrivalAddressModel = [self.order arrivalAddressModel];
        [items addObject:[UUFormItemMode itemWithTitle:@"到达地址" content:arrivalAddressModel.address]];
        
        if (self.order.type == UUOrderTypeInstant) {
            [items addObject:[UUFormItemMode itemWithTitle:@"途经地址"
                                                   content:[[self.order.approach.toAddressModels valueForKey:@"address"]
                                                            componentsJoinedByString:@"\n"]]];
        } else {
            [items addObject:[UUFormItemMode itemWithTitle:@"规格" content:self.order.specInfo]];
        }
        
        [items addObject:[UUFormItemMode itemWithTitle:@"天数" content:[NSString daysToDisplayString:self.order.days]]];
    } else { // 按线路
        [items addObject:[UUFormItemMode itemWithTitle:@"线路名称" content:self.order.line]];
    }
    
    [items addObject:[UUFormItemMode itemWithTitle:@"数量" content:[NSString stringWithFormat:@"%ld辆", self.order.carNum]]];
    
    if (self.order.type == UUOrderTypeInstant && self.order.extra.length > 0) {
        [items addObject:[UUFormItemMode itemWithTitle:@"备注" content:self.order.extra]];
    }
    
    UUFormView *formView1 = [[UUFormView alloc] initWithItems:items preferredMaxLayoutWidth:formPreferredMaxLayoutWidth];
    [self.view addSubview:formView1];
    [formView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(carTypeLabel.mas_bottom).offset(28);
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
    }];
    
    [items removeAllObjects];
    
    if ([self needsToShowReservedContact]) {
        [self.view insertDashline:18];
        
        [items addObject:[UUFormItemMode itemWithTitle:@"预定人" content:self.order.contact]];
        [items addObject:[UUFormItemMode itemWithTitle:@"联系电话" content:self.order.contactMobile]];
        
        UUFormView *formView2 = [[UUFormView alloc] initWithItems:items preferredMaxLayoutWidth:formPreferredMaxLayoutWidth];
        [self.view addSubview:formView2];
        [formView2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view.last2View.mas_bottom).offset(17);
            make.left.right.equalTo(formView1);
        }];
        
        [items removeAllObjects];
    }
    
    if (self.order.type == UUOrderTypeInstant) {
        UIButton *intentOrderDetailButton = [UIButton buttonWithType:UIButtonTypeCustom];
        intentOrderDetailButton.titleLabel.font = [GetResourceManager() iconFontWithSize:14];
        [intentOrderDetailButton setTitle:GetResourceManager().seeIntentOrderDetailIconText forState:UIControlStateNormal];
        [intentOrderDetailButton setTitleColor:COMMON_TEXT_LIGHT_COLOR forState:UIControlStateNormal];
        [intentOrderDetailButton addTarget:self action:@selector(enterIntentOrderDetail) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:intentOrderDetailButton];
        [intentOrderDetailButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(30);
            make.left.right.mas_equalTo(0);
            make.top.equalTo(self.view.last2View.mas_bottom).offset(10);
        }];
    }
    
    if (self.order.refundStatus == UUOrderRefundStatusInRefunding || self.order.status == UUOrderStatusRefunded) { // 退款中或已退款
        [self.view insertDashline:18];
        
        [items addObject:[UUFormItemMode itemWithTitle:@"退款原因" content:self.order.refusalReason]];
        [items addObject:[UUFormItemMode itemWithTitle:@"退款时间" content:[self.order.refundTime formatDateByFormatString:@"yyyy.MM.dd HH:mm"]]];
        
        if (self.order.status == UUOrderRefundStatusInRefunding) { // 退款中
            [items addObject:[UUFormItemMode itemWithTitle:@"退款状态" content:@"待审核"]];
        } else { // 已退款
            [items addObject:[UUFormItemMode itemWithTitle:@"退款金额" content:[self.order.refundMoney toCurrency]]];
        }
        
        UUFormView *formView3 = [[UUFormView alloc] initWithItems:items preferredMaxLayoutWidth:formPreferredMaxLayoutWidth];
        [self.view addSubview:formView3];
        [formView3 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view.last2View.mas_bottom).offset(17);
            make.left.right.equalTo(formView1);
        }];
    }
    
    [self.view.subviews.lastObject mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-19);
    }];
}

- (BOOL)needsToShowReservedContact {
    return self.order.status == UUOrderStatusWaitingForAssign
    || self.order.status == UUOrderStatusWaitingForService
    || self.order.status == UUOrderStatusInService
    || self.order.status == UUOrderStatusComplete
    || self.order.refundStatus == UUOrderRefundStatusInRefunding
    || self.order.status == UUOrderStatusRefunded
    || self.order.status == UUOrderStatusCancelled;
}

- (void)enterIntentOrderDetail {
    if (self.order.intentOrderId != nil) {
        UUIntentOrderDetailVC *detailVC = [UUIntentOrderDetailVC instanceOfAppropriate];
        detailVC.intentOrderId = self.order.intentOrderId;
        [self.navigationController pushViewController:detailVC animated:YES];
    }
}

@end
